package org.android.bbdroid.guiutils;

import java.net.URL;

import org.android.bbdroid.webservice.PHPBBClient;

import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;

public final class HTMLUtils
{
  private static Html.ImageGetter getter = new PHPBBImageGetter();

  public static Spanned fromHTML(String text)
  {
    return Html.fromHtml(text, getter, null);
  }

  private static class PHPBBImageGetter
    implements Html.ImageGetter
  {
    public Drawable getDrawable(String url)
    {
      try
      {
    	  URL u=url.startsWith(".")? new URL(PHPBBClient.getInstance().getEndpoint(), url) : new URL(url);
    	  
        Drawable d = Drawable.createFromStream(u.openStream(), url);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
      }
      catch (Exception e)
      {
        return null;
      }
    }
  }
}